package com.catazy.ocrdemo.ui.activity.main

import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Bundle
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.catazy.mvvmlib.base.activity.BaseVmVbActivity
import com.catazy.mvvmlib.ext.view.click
import com.catazy.ocrdemo.databinding.ActivityMainBinding
import com.catazy.ocrdemo.manager.OcrManager
import com.catazy.ocrdemo.ui.activity.scan.AppScanActivity
import com.catazy.ocrdemo.ui.dialog.CommonDialog
import com.googlecode.tesseract.android.TessBaseAPI

class MainActivity : BaseVmVbActivity<MainViewModel, ActivityMainBinding>() {

    private val mDialog by lazy { CommonDialog(this) }

    override fun initView(savedInstanceState: Bundle?) {
//        mViewModel.saveTrainedDataToFile(this)
        // 检查权限
        checkPermission()
        // OCR识别监听器
        orcRecognizeListener()
        // 初始化OpenCV
        mViewModel.initOpenCv()

        mDatabind.btn.click {
            // 识别图片
//            mViewModel.recognizeImage(resources)
            startActivity(Intent(this, AppScanActivity::class.java))
//            startActivity(Intent(this, TestScanActivity::class.java))
        }
    }

    private fun checkPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            LogUtils.i("android 12版本及以下")
            if (!PermissionUtils.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                PermissionUtils.permission(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
                ).callback(object : PermissionUtils.FullCallback {
                    override fun onGranted(permissionsGranted: List<String>) {
                        LogUtils.i("android 12版本及以下权限申请成功")
                        // 初始化OCR训练模型
                        mViewModel.saveTrainedDataToFile(this@MainActivity)
                    }

                    override fun onDenied(
                        permissionsDeniedForever: List<String>,
                        permissionsDenied: List<String>
                    ) {
                        LogUtils.i("android 12版本及以下权限申请失败")
                    }
                }).request()
            } else {
                // 初始化OCR训练模型
                mViewModel.saveTrainedDataToFile(this@MainActivity)
            }
        } else {
            LogUtils.i("android 13版本及以上")
            if (!PermissionUtils.isGranted(Manifest.permission.READ_MEDIA_IMAGES)) {
                PermissionUtils.permission(
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_AUDIO,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.CAMERA
                ).callback(object : PermissionUtils.FullCallback {
                    override fun onGranted(permissionsGranted: List<String>) {
                        LogUtils.i("android 13版本及以上权限申请成功")
                        // 初始化OCR训练模型
                        mViewModel.saveTrainedDataToFile(this@MainActivity)
                    }

                    override fun onDenied(
                        permissionsDeniedForever: List<String>,
                        permissionsDenied: List<String>
                    ) {
                        LogUtils.i("android 13版本及以上权限申请失败")

                    }
                }).request()
            } else {
                // 初始化OCR训练模型
                mViewModel.saveTrainedDataToFile(this@MainActivity)
            }
        }


    }

    private fun orcRecognizeListener() {
        // OCR识别监听器
        OcrManager.INSTANCE.addListener(object : OcrManager.OcrRecognizeListener {
            override fun onStart() {
            }

            override fun onSuccess(result: String?) {
            }

            override fun onFail(t: Throwable) {
            }

            override fun onProgress(p0: TessBaseAPI.ProgressValues) {
            }
        })
    }


    override fun onStop() {
        super.onStop()
        // 释放OCR训练模型
        OcrManager.INSTANCE.stopTess()
    }

}